package com.gcd.tempe;

import com.gcd.tempe.util.JSON_Response;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LihatItemHistoryPesananActivity extends Activity {

	private final static String SEMUA_S = "Semua";
	private final static String BELUM_BAYAR_S = "Belum dibayar";
	private final static String SUDAH_KONFIRM_S = "Sudah konfirmasi bayar";
	private final static String SUDAH_BAYAR_S = "Sudah dibayar";
	private final static String SUDAH_TIBA_S = "Barang sudah tiba";
	
	private final static String[] STATUS_S = new String[]{
		SEMUA_S, BELUM_BAYAR_S, SUDAH_KONFIRM_S, SUDAH_BAYAR_S, SUDAH_TIBA_S
	};
	
	private int orderid;
	private String username;
	private String password;
	private int orderstatus;
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_history);
		
		Bundle bundle = getIntent().getExtras();
		
		if(bundle.getString("orderid") != null){
			
			orderid = Integer.parseInt(bundle.getString("orderid"));
			username = bundle.getString("username");
			password = bundle.getString("password");
			
			
			TextView orderIdTV = (TextView) findViewById(R.id.orderid);
			orderIdTV.setText("No Pesanan : " + bundle.getString("orderid"));
			
			TextView ordertimeTV = (TextView) findViewById(R.id.orderdate);
			ordertimeTV.setText("Tanggal Pesan : "+bundle.getString("orderdate"));
			
			TextView orderstatusTV = (TextView) findViewById(R.id.orderstatus);
			orderstatus =Integer.parseInt( bundle.getString("orderstatus"));
			orderstatusTV.setText("Status : " + STATUS_S[orderstatus]);
			
			if (orderstatus != 1) {
				//Hilangkan button confirm pembayaran
				View b = findViewById(R.id.button1);
	        	b.setVisibility(View.GONE);
			}
		}else{
			Log.d("pret", "orderId == null");
		}
	}
	
	public void confirmpayment (View v){
		try {
			String confirmStatus = JSON_Response.confirmPayment(username, password, orderid);
			if (confirmStatus.endsWith("success")){
				Toast succesMsg = Toast.makeText(this, "Konfirmasi berhasil", Toast.LENGTH_LONG);
				succesMsg.show();
				TextView orderstatusTV = (TextView) findViewById(R.id.orderstatus);
				if (orderstatus == 1){
					orderstatusTV.setText("Status : " + STATUS_S[2]);	
				}
			} else{
				Toast failedMsg = Toast.makeText(this, "Konfirmasi gagal", Toast.LENGTH_LONG);
				failedMsg.show();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}		
	}
}
